package crud

import (
	"github.com/chinahdkj/xorm"
)

//IActionService
type IActionService interface {
	DB() *xorm.Engine
	SetDB(*xorm.Engine)
	Initializing() error
}

type ActionService struct {
	db   *xorm.Engine
	from func(s *ActionService) *xorm.Session
}

type SimpleQueryFunc func(m *CQuery) (interface{}, error)

func NewAction(db *xorm.Engine) ActionService {
	act := ActionService{db: db}
	return act
}

func (s *ActionService) contract() IActionService {
	return s
}

func (s *ActionService) DB() *xorm.Engine {
	return s.db
}

func (s *ActionService) SetDB(db *xorm.Engine) {
	s.db = db
}

func (s *ActionService) Initializing() error { return nil }

func (s *ActionService) From() *xorm.Session {

	if s.from != nil {
		return s.from(s)
	}

	return s.db.NewSession()
}

func (s *ActionService) SetFrom(from func(s *ActionService) *xorm.Session) {
	s.from = from
}
